package net.solosky.easyavatar.utils;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import net.solosky.easyavatar.bean.ProviderMeta;
import net.solosky.easyavatar.utils.AsyncTask.Status;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class Utils {
	
	public static boolean debugFlag = true;
	public static String nLine = System.getProperty( "line.separator" );
	public static int WC = LayoutParams.WRAP_CONTENT;
	public static int FP = LayoutParams.FILL_PARENT;
	public static LinearLayout.LayoutParams paramWC = new LinearLayout.LayoutParams(WC, WC);
	public static LinearLayout.LayoutParams paramFP = new LinearLayout.LayoutParams(FP, FP);
	
	
	/**
	 * 打印log
	 * @param obj
	 */
	public static void dout(Object obj){ 
		if(debugFlag) {
			Log.d("[dout]","obj>>>>>>>>>>>>>"+obj.getClass().getName()+">>"+obj.toString());
		}
	}
	public static void dout(int obj){
		if(debugFlag) {
			Log.d("[dout]","int>>>>>>>>>>>>>"+obj); 
		}
	}
	public  static void dout(String str){
		if(debugFlag) {
			Log.d("[dout]","str>>>>>>>>>>>>>"+str);
		}
	}
	public  static void dout(String str,String str2){
		if(debugFlag) {
			Log.d("[dout]","str>>>>>>>>>>>>>"+str+" "+str2);
		}
	}
	public  static void dout(String[] str){
		if(debugFlag){
		for(int i=0; i<str.length; i++) {
			Log.d("[dout]","str["+i+"]>>>>>>>>>>>>>"+str[i]);
		}
		}
	}
	
	public static void dout(Throwable t){
		dout(Log.getStackTraceString(t));
	}

	public static String getDisplayDateStr(String dateStr){
		//dateStr = "Thu Apr 30 01:33:41 +0000 2009";
		Date dd = new Date(dateStr);
		SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); 
		String str = "";
		try{
			str = myFmt.format(dd);			
		}catch(Exception e){
			e.printStackTrace();
		}
		dout(str);
		return str;
	}
	
	public static String getDateStr(String dateStr){
		Date date = str2Date(dateStr);
		return getHowLongStr(date);		
	}
	
	public static String getDateStr(Long l){
		Date date = long2Date(l);
		return getHowLongStr(date);		
	}
	
	public static Date long2Date(Long l){
		Date d = new Date();
		d.setTime(l);
		return d;
	}
	
	//输入"Thu Apr 30 01:33:41 +0000 2009" 返回Date
	public static Date str2Date(String dateStr){
		return new Date(dateStr);
	}
	
	//传入一个Date，判断是多久前，返回示例"4小时前"
	public static String getHowLongStr(Date date){
		String rs = "刚刚";
		Long i = date.getTime();
		Date now = new Date();
		Long j = now.getTime();
		//dout(j - i);
		long day = 1000 * 60 * 60 * 24;
		long hour = 1000 * 60 * 60;
		long min = 1000 * 60;
		long sec = 1000;
		if (((j-i)/day)>0)
			rs = ((j-i)/day)+"天前";
		else if (((j-i)/hour)>0)
			rs = ((j-i)/hour)+"小时前";
		else if (((j-i)/min)>0)
			rs = ((j-i)/min)+"分钟前";
		else if (((j-i)/sec)>0)
			rs = ((j-i)/sec)+"秒前";
		return rs;
	}
	
	public static Long dateStr2Long(String dateStr){
		if(TextUtils.isEmpty(dateStr))
			return new Date().getTime();
		return  new Date(dateStr).getTime();
	}
	
	
	
	public static String downFileToLocalPath(String url){
		String localPath = "";
		
		return localPath;
	}
	
	/**将Bitmap转成文件放在程序文件目录下*/
	public static void Bitmap2File(Bitmap bitmap, String fileName, Context ctx) {
		
		try {
			FileOutputStream fos = ctx.openFileOutput(fileName, android.content.Context.MODE_WORLD_READABLE);
			bitmap.compress(CompressFormat.JPEG, 80, fos);			
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	
	
	public static String appendSql(String [] columns , String sql){
		
		StringBuffer sb = new StringBuffer();		
		int l = columns.length;
		for(int i=0;i<l;i++){
			String s = columns [i];
			if(i==l-1){
				sb.append(s);
			}else{
				sb.append(s+",");
			}
		}
		sql = sql.replace("*", sb.toString());
		return sql;
	}	
	
	
	/**
	 * 判读是否有加载sdcard
	 * @return
	 */
	public static Boolean hasSdCard(){
		Environment  en =  new Environment();
		if ( !android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) ) {
			return false;
		}
		return true;
	}
	
	/**
	 * 判读是否有加载sdcard
	 * @return
	 */
	
	public static boolean hasEnoughSpace(long fileLengh) {
		return (getSdCardSpace() > fileLengh);
	}
	
	/**
	 * 获取sdcard剩余空间 单位为byte 1024*1024 byte = 1024 KB = 1 MB
	 * @return
	 */
	
	public static long getSdCardSpace() {
		File path = Environment.getExternalStorageDirectory();
		StatFs stat = new StatFs(path.getPath());
		long availableBlocks = stat.getAvailableBlocks();
		
		return availableBlocks*stat.getBlockSize();
	}
	
	/**
	 * 判断字符串是否是空字符串
	 * @param s
	 * @return
	 */
	public static boolean isEmptyString(String s){
		return s==null || s.length()==0 || s.trim().length()==0;
	}
	
	/**
	 * 安全的取消一个任务
	 * @param task
	 */
	public static void safeCancelTask(AsyncTask task){
		if(task!=null && !task.isCancelled() &&
				task.getStatus()!=Status.FINISHED){
			task.cancel(true);
		}
	}
	
	public static ProviderMeta getProviderMeta(String code){
		Iterator<ProviderMeta> it = Globals.PROVIDER_META_LIST.iterator();
		while(it.hasNext()){
			ProviderMeta meta = it.next();
			if(meta.getCode().equals(code)){
				return meta;
			}
		}
		return null;
	}
}
